<!DOCTYPE html>
<html>
	<head>
		<title>添加评价</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
		<style>
			body {
				font-family: Arial, sans-serif;
				max-width: 800px;
				margin: 20px auto;
				padding: 20px;
				background-color: #f5f5f5;
			}
			.review-form {
				background-color: white;
				padding: 20px;
				border-radius: 8px;
				box-shadow: 0 2px 4px rgba(0,0,0,0.1);
			}
			.form-group {
				margin-bottom: 20px;
			}
			label {
				display: block;
				margin-bottom: 5px;
				font-weight: bold;
			}
			input[type="text"], 
			textarea {
				width: 100%;
				padding: 8px;
				border: 1px solid #ddd;
				border-radius: 4px;
				box-sizing: border-box;
			}
			textarea {
				height: 100px;
				resize: vertical;
			}
			.rating {
				display: flex;
				flex-direction: row-reverse;
				justify-content: flex-end;
			}
			.rating > input {
				display: none;
			}
			.rating > label {
				position: relative;
				width: 1.1em;
				font-size: 30px;
				color: #FFD700;
				cursor: pointer;
			}
			.rating > label::before {
				content: "\2605";
				position: absolute;
				opacity: 0;
			}
			.rating > label:hover:before,
			.rating > label:hover ~ label:before {
				opacity: 1 !important;
			}
			.rating > input:checked ~ label:before {
				opacity: 1;
			}
			.submit-btn {
				background-color: #4CAF50;
				color: white;
				padding: 10px 20px;
				border: none;
				border-radius: 4px;
				cursor: pointer;
				font-size: 16px;
			}
			.submit-btn:hover {
				background-color: #45a049;
			}
			.error-message {
				color: red;
				margin-top: 5px;
				font-size: 14px;
				display: none;
			}
		</style>
	</head>
	<body>
		<div class="review-form">
			<h2>添加商品评价</h2>
			<form id="reviewForm">
				<div class="form-group">
					<label>订单编号</label>
					<input type="text" id="orderId" name="orderId" required>
					<div class="error-message" id="orderIdError"></div>
				</div>
				
				<div class="form-group">
					<label>评分</label>
					<div class="rating">
						<input type="radio" id="star5" name="rating" value="5" required>
						<label for="star5">★</label>
						<input type="radio" id="star4" name="rating" value="4">
						<label for="star4">★</label>
						<input type="radio" id="star3" name="rating" value="3">
						<label for="star3">★</label>
						<input type="radio" id="star2" name="rating" value="2">
						<label for="star2">★</label>
						<input type="radio" id="star1" name="rating" value="1">
						<label for="star1">★</label>
					</div>
					<div class="error-message" id="ratingError"></div>
				</div>

				<div class="form-group">
					<label>评价内容</label>
					<textarea id="comment" name="comment" required></textarea>
					<div class="error-message" id="commentError"></div>
				</div>

				<button type="submit" class="submit-btn">提交评价</button>
			</form>
		</div>

		<script>
			document.getElementById('reviewForm').addEventListener('submit', async function(e) {
				e.preventDefault();
				
				// 重置错误信息
				document.querySelectorAll('.error-message').forEach(el => el.style.display = 'none');
				
				// 获取表单数据
				const formData = {
					orderId: parseInt(document.getElementById('orderId').value),
					rating: parseInt(document.querySelector('input[name="rating"]:checked')?.value || 0),
					comment: document.getElementById('comment').value.trim()
				};

				// 验证
				let hasError = false;
				if (!formData.orderId) {
					document.getElementById('orderIdError').textContent = '请输入订单编号';
					document.getElementById('orderIdError').style.display = 'block';
					hasError = true;
				}
				if (!formData.rating) {
					document.getElementById('ratingError').textContent = '请选择评分';
					document.getElementById('ratingError').style.display = 'block';
					hasError = true;
				}
				if (!formData.comment) {
					document.getElementById('commentError').textContent = '请输入评价内容';
					document.getElementById('commentError').style.display = 'block';
					hasError = true;
				}

				if (hasError) return;

				try {
					const response = await fetch('http://localhost:8080/review/add', {
						method: 'POST',
						headers: {
							'Content-Type': 'application/json'
						},
						body: JSON.stringify(formData)
					});

					if (response.ok) {
						alert('评价提交成功！');
						document.getElementById('reviewForm').reset();
					} else {
						alert('评价提交失败，请重试！');
					}
				} catch (error) {
					console.error('Error:', error);
					alert('提交时发生错误，请稍后重试！');
				}
			});
		</script>
	</body>
</html>
